/**
 * @brief 与 MCU 内核 相关的实现
 * @author 张勇 / 2020-05-13
*/

#include "mcu.h"

// x86 架构的处理器采用小端序（Little-endian）
bool mcu_is_big_endian() { return false; }

size_t mcu_align_bytes() {
	// x86 4 字节对齐时寻址速度最快
	return 4;
}

uint64_t mcu_get_beu64(const void *stream) {
	uint8_t *buf = (uint8_t*)stream;
	return ((uint64_t)buf[0] << 56) | ((uint64_t)buf[1] << 48) | ((uint64_t)buf[2] << 40) | ((uint64_t)buf[3] << 32) |
	       ((uint64_t)buf[4] << 24) | ((uint64_t)buf[5] << 16) | ((uint64_t)buf[6] << 8 ) | ((uint64_t)buf[7]);
}

uint32_t mcu_get_beu32(const void *stream) {
	uint8_t *buf = (uint8_t*)stream;
	return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3]);
}

uint16_t mcu_get_beu16(const void *stream) {
	uint8_t *buf = (uint8_t*)stream;
	return (buf[0] << 8) | (buf[1]);
}

uint64_t mcu_get_leu64(const void *stream) {
	uint8_t *buf = (uint8_t*)stream;
	return ((uint64_t)buf[7] << 56) | ((uint64_t)buf[6] << 48) | ((uint64_t)buf[5] << 40) | ((uint64_t)buf[4] << 32) |
	       ((uint64_t)buf[3] << 24) | ((uint64_t)buf[2] << 16) | ((uint64_t)buf[1] << 8 ) | ((uint64_t)buf[0]);
}

uint32_t mcu_get_leu32(const void *stream) {
	uint8_t *buf = (uint8_t*)stream;
	return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0]);
}

uint16_t mcu_get_leu16(const void *stream) {
	uint8_t *buf = (uint8_t*)stream;
	return (buf[1] << 8) | (buf[0]);
}

void* mcu_get_pointer(const void *stream) {
	// x86 架构的处理器采用小端序（Little-endian）
	switch(mcu_pointer_size()) {
	case 8:  return (void*)(uintptr_t)mcu_get_leu64(stream);
	case 4:  return (void*)(uintptr_t)mcu_get_leu32(stream);
	default: return (void*)(uintptr_t)mcu_get_leu16(stream);
	}
}

void mcu_watchdog_feed(void) {
	// 不支持
}
